package com.gujiayue.module.sys.dao.sql;

import org.apache.ibatis.jdbc.SQL;

import com.gujiayue.common.utils.StringUtils;
import com.gujiayue.module.sys.pojo.SysUsrDo;

public class SysUsrSqlBuilder {
	
	
	public String countPersonSql(final SysUsrDo sysUsrDo) {
		return new SQL() {
			{
				SELECT(" count(*) AS pageCount");
				FROM("sys_usr a");
				if(StringUtils.isNotEmpty(sysUsrDo))
				if (StringUtils.isNotBlank(sysUsrDo.getUserId())) {
					WHERE("a.user_id = #{userId}");
				}
				if (StringUtils.isNotBlank(sysUsrDo.getLoginName())) {
					WHERE("a.login_name like #{loginName}");
				}
			}
		}.toString();
	}
	
	
	public String selectPersonSql(final SysUsrDo sysUsrDo) {
		return new SQL() {
			{
				SELECT("a.id AS id");
				SELECT("a.login_name AS loginName");
				SELECT("a.password AS password");
				SELECT("a.name AS name");
				SELECT("a.email AS email");
				SELECT("a.mobile AS mobile");
				SELECT("a.phone AS phone");
				SELECT("a.del_flag AS del_flag"); 
				FROM("sys_usr a");
				if(StringUtils.isNotEmpty(sysUsrDo)) {
					if (StringUtils.isNotBlank(sysUsrDo.getUserId())) {
						WHERE("a.user_id = #{userId}");
					}
					if (StringUtils.isNotBlank(sysUsrDo.getLoginName())) {
						WHERE("a.login_name like #{loginName}");
					}
					
					ORDER_BY("a.create_date");
					
//					if (StringUtils.isNotBlank(sysUsrDo.getPageSql())) {
//						//LIMIT(value)("a.id = #{pageSql}");
//					}
				}
			}
		}.toString();

	}

	public String deletePersonSql(final SysUsrDo sysUsrDo) {
		return new SQL() {
			{
				DELETE_FROM("sys_usr");
				WHERE("id = #{id}");
			}
		}.toString();
	}

	public String insertPersonSql(final SysUsrDo sysUsrDo) {
		return new SQL() {
			{
				INSERT_INTO("sys_usr");
				VALUES("id", "#{id}");
				VALUES("login_name", "#{loginName}");
				VALUES("password", "#{password}");
				VALUES("name", "#{name}");
				VALUES("email", "#{email}");
				VALUES("mobile", "#{mobile}");
				VALUES("phone", "#{phone}");
				VALUES("del_flag", "#{delFlag}");
			}
		}.toString();
	}

	public String updatePersonSql(final SysUsrDo sysUsrDo) {
		return new SQL() {
			{
				UPDATE("sys_usr");
				SET("id = #{id}");
				SET("login_name = #{loginName}");
				SET("password = #{password}");
				SET("name = #{name}");
				SET("email = #{email}");
				SET("mobile = #{mobile}");
				SET("phone = #{phone}");
				SET("del_flag = #{delFlag}");
				WHERE("id = #{id}");
			}
		}.toString();
	}
}
